package cn.taketoday.live.thread.core;

import cn.taketoday.live.config.GlobalData;
import cn.taketoday.live.service.impl.ClientServiceImpl;
import cn.taketoday.live.utils.SpringUtils;

/**
 * @author BanqiJane
 * @ClassName ReConnThread
 * @Description TODO
 * @date 2020年8月10日 下午12:29:52
 * @Copyright:2020 blogs.acproject.xyz Inc. All rights reserved.
 */
public class ReConnThread extends Thread {
  public volatile boolean RFLAG = false;
  private volatile Integer num = 0;
  private ClientServiceImpl clientService = SpringUtils.getBean(ClientServiceImpl.class);
  private int TIME = 10000;

  @Override
  public synchronized void run() {
    // TODO 自动生成的方法存根
    super.run();
    while (!RFLAG) {
      try {
        Thread.sleep(TIME);
      }
      catch (InterruptedException e) {
        e.printStackTrace();
      }
      if (RFLAG) {
        return;
      }
      if (num > 20) {
        this.TIME = 300000;
//				RFLAG=true;
//				num=0;
//				return;
      }
      if (!GlobalData.webSocketProxy.isOpen()) {
        try {
          clientService.reConnService();
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        num++;
        System.out.println("每" + this.TIME / 1000 + "秒,进行重连第" + num + "次(來源於綫程重連機制beta1.2)");
      }
      else {
        num = 0;
        RFLAG = true;
        return;
      }
    }
  }

}
